package cc.autoapi.pucong.liteflow.design.core.configuration;

import cc.autoapi.pucong.liteflow.design.mobal.JsonBean;
import cc.autoapi.pucong.liteflow.design.core.exception.DesignException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 接口异常处理器
 *
 * @author pucong
 */
public interface DesignExceptionHandler {

    Logger logger = LoggerFactory.getLogger(DesignExceptionHandler.class);

    /**
     * 接口异常处理
     *
     * @param e 异常对象
     * @return 返回json对象
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    default Object exceptionHandler(Exception e) {
        logger.error("api调用接口出错", e);
        return new JsonBean<>(-1, e.getMessage());
    }

    /**
     * 接口异常处理
     *
     * @param e 异常对象
     * @return 返回json对象
     */
    @ExceptionHandler(DesignException.class)
    @ResponseBody
    default Object exceptionHandler(DesignException e) {
        return new JsonBean<>(500, e.getMsg());
    }
}